package com.eascs.spider.yuqing.worker.spider;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.eascs.spider.yuqing.common.proj.Platform;
import com.eascs.spider.yuqing.common.proj.Task;
import com.eascs.spider.yuqing.common.util.NetUtil;
import com.eascs.spider.yuqing.worker.util.MongodbUtil;

/**
 * 凤凰资讯
 * @author hzp
 *
 */
public class IfengNewsSpider extends SimpleSpider{

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
	private String time;
	
	public IfengNewsSpider(String url) {
		super(url);
		this.time = sdf.format(new Date(System.currentTimeMillis()));
	}

	@Override
	public void run(){
		Map<String, String> header = new HashMap<String, String>();
		header.put("Host", "news.ifeng.com");
		String html = NetUtil.loadPageRawTextByGet(url, client, header, null, "utf-8");
		extractUrl(html);
		if(!url.equals("http://news.ifeng.com/"))
			extractInfo(html);
	}
	
	public void extractUrl(String html){
		Pattern pattern = Pattern.compile("http://news.ifeng.com/a.*?.shtml");
		Matcher matcher = pattern.matcher(html);
		while(matcher.find()){
			String url = matcher.group();
			if(!url.contains(time))
				continue;
			Task task = new Task();
			task.setUrl(url);
			manager.addSendQueue(task);
		}
	}
	
	public void extractInfo(String html){
		try {
			Map<String, Object> data = new HashMap<>();
			Document document = Jsoup.parse(html);
			if(document.title().contains("404-页面不存在"))
				return;
			//判断是否图片新闻，否则一般性处理
			if(document.select("meta[itemprop=image]").size() != 0){
				extractInfoFromPictureNews(html);
				return ;
			}
			
			//标题
			String title = document.select("h1").text();
			//来源
			String source = document.select("span[itemprop=publisher]").text();
			//时间
			String time = null;
			if(document.select("span[itemprop=datePublished]").size() != 0){
				time = document.select("span[itemprop=datePublished]").text();
			}else{
				time = document.select("div.yc_tit span").get(0).text();
				source = document.select("div.yc_tit a").get(0).text();
			}
			
			//内容
			Element contentEle = document.getElementById("main_content");
			if(contentEle == null){
				contentEle = document.getElementById("yc_con_txt");
			}
			
			String content = contentEle.text();
			logger.info("title:{} ,time{}, source:{},content:{}" , title ,time , source ,content);
			data.put("title", title);
			data.put("time", time);
			data.put("source", source);
			data.put("content", content);
			data.put("url", url);
			data.put("crawler_time", new Date(System.currentTimeMillis()));
			MongodbUtil.setYuqing(data, Platform.IFENG_NEWS);
		} catch (Exception e) {
			logger.error("html解析异常,url:{}" ,url , e);
		}
	}
	
	//从图片新闻中提取信息
	public void extractInfoFromPictureNews(String html){
		Document document = Jsoup.parse(html);
		String title = document.select("h1").text();
		String time = document.select("div#titL span").get(0).text();
		html = html.substring(html.indexOf("var share_data"), html.indexOf("return  share_data"));
		StringBuffer sb = new StringBuffer();
		Pattern pattern = Pattern.compile("title:'(.*?)'");
		Matcher matcher = pattern.matcher(html);
		while(matcher.find()){
			sb.append(matcher.group(1));
		}
		logger.info("title:{},time:{} source:凤凰图片新闻,content:{}" ,title , time , sb.toString());
		Map<String , Object> data = new HashMap<>();
		data.put("title", title);
		data.put("time", time);
		data.put("source", "凤凰图片新闻");
		data.put("content", sb.toString());
		data.put("url", url);
		data.put("crawler_time", new Date(System.currentTimeMillis()));
		MongodbUtil.setYuqing(data, Platform.IFENG_NEWS);
	}
}
